<template>
  <view class="cabinetDetail">
    <!-- <nut-watermark :z-index="999" content="小刘"></nut-watermark> -->
    <scroll-view 
        style="height: 100%;"
        :scroll-y="true"
      >
      <nut-cell-group title="设备详情" desc="">
        <nut-cell title="场地名称">
          <template #desc>
            <span>{{cabinetDetailData.f_field_name}}</span>
          </template>
        </nut-cell>
        <nut-cell title="设备名称">
          <template #desc>
            <span>{{cabinetDetailData.c_cabinet_name}}</span>
          </template>
        </nut-cell>
        <nut-cell title="设备编码">
          <template #desc>
            <span>{{cabinetDetailData.c_code}}</span>
          </template>
        </nut-cell>
        <nut-cell title="安装地址">
          <template #desc>
            <span>{{cabinetDetailData.c_install_area}}</span>
          </template>
        </nut-cell>
        <nut-cell title="在线状态">
          <template #desc>
            <nut-tag v-if="cabinetDetailData.c_activate == 1" type="success">在线</nut-tag>
            <nut-tag v-else-if="cabinetDetailData.c_activate == -1" type="warning">离线</nut-tag>
          </template>
        </nut-cell>
        <nut-cell title="最后在线时间">
          <template #desc>
            <span>{{cabinetDetailData.c_last_end_time}}</span>
          </template>
        </nut-cell>
        <nut-cell title="设备组数">
          <template #desc>
            <span>{{cabinetDetailData.c_cabinet_num}}</span>
          </template>
        </nut-cell>
        <nut-cell title="眼镜层数">
          <template #desc>
            <span>{{cabinetDetailData.c_glasses_layer}}</span>
          </template>
        </nut-cell>
        <nut-cell title="每层组数">
          <template #desc>
            <span>{{cabinetDetailData.c_layer_group}}</span>
          </template>
        </nut-cell>
        <nut-cell title="每组数量">
          <template #desc>
            <span>{{cabinetDetailData.c_group_glasses_num}}</span>
          </template>
        </nut-cell>
      </nut-cell-group>
      <nut-cell-group title="操作设备" desc="">
        <nut-cell title="打开机柜门">
          <template #desc>
            <nut-button :type="cabinetDetailData.c_activate == -1 ? 'default' : 'warning'" size="small" :disabled="cabinetDetailData.c_activate == -1" @click="openTheCabinet">打开</nut-button>
          </template>
        </nut-cell>
      </nut-cell-group>
      <nut-tabs v-model="cabinetNum" type="smile" @click="changeTabGetData">
        <nut-tab-pane :title="'设备' + item" :pane-key="item" v-for="item in totalCabinet" >
          <nut-cell-group>
            <nut-cell :title="'货道'  + cabinetItem.p_freight_lane + '('+ cabinetItem.p_glasses_type + ')'" v-for="cabinetItem in cabinetDataList">
              <template #desc>
                {{ cabinetItem.p_existing_number }} / {{ cabinetItem.p_max_number }}
              </template>
            </nut-cell>
          </nut-cell-group>
          </nut-tab-pane>
      </nut-tabs>
    </scroll-view>
  </view>
</template>

<script setup>
import './index.scss';
import { ref } from 'vue';
import Taro, {useDidShow} from '@tarojs/taro'
import {query_equipment_params, do_equipment_open_door} from '../../https/api'

const cabinetDetailData = ref({})
const cabinetNum = ref(1)
const totalCabinet = ref(1)
const PCCode = ref('')
const cabinetDataList = ref([])

const changeTabGetData = (data) => {
  getCurrentData(PCCode.value, data.paneKey)
}

const getCurrentData = (code, index) => {
  query_equipment_params({p_c_code: code, p_group_code: index}).then(res => {
    // [{
    //   p_group_code: 1,
    //   p_glasses_type: '成人款',
    //   p_existing_number: 10,
    //   p_max_number: 21
    // }]
    cabinetDataList.value = res.data
  })
}
const openTheCabinet = () => {
    Taro.showModal({
      title: '温馨提示',
      confirmText: '开锁',
      content: '您确认要打开设备门？',
      success: (res) => {
        if (res.confirm) {
          do_equipment_open_door({c_code: PCCode.value}).then(res => {
              if(res.code === '1000' && res.data) {
                  Taro.showToast({
                    title: '打开成功',
                    icon: 'success',
                    duration: 2000,
                  })
              } else {
                Taro.showToast({
                    title: '打开失败',
                    icon: 'none',
                    duration: 2000,
                })
              }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
}
useDidShow(() => {
  let params =Taro.getCurrentInstance().router.params
  // let tempStr =  '{"c_activate":-1,"c_cabinet_name":"123","c_cabinet_num":3,"c_code":"EO202406232156234920105xjj4p","c_coomperation_code":"CO202406232147036870239lf560","c_coomperation_name":null,"c_f_code":"Fo20240623215548611440jwa8o5","c_glasses_layer":5,"c_group_glasses_num":6,"c_heartbeat_code":"6601062024062321562E","c_install_area":"河南郑州万达","c_layer_group":6,"c_manufacturer":null,"c_register_code":"6601062024062321562D","c_status":1,"create_time":"2024-06-23 21:56:23","creator_id":"","f_field_name":"河南郑州万达","update_time":"2024-06-23 22:36:33"}'
  // console.log(JSON.parse(tempStr))
  // params.data
  cabinetDetailData.value = JSON.parse(params.data)
  totalCabinet.value = cabinetDetailData.value.c_cabinet_num
  PCCode.value =  cabinetDetailData.value.c_code
  getCurrentData(PCCode.value, 1)


})
</script>

<style>
</style>